﻿using DTOs;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using RBAC.Domain;
using RBAC.Repository;
using RBACDuYaJun.Api.Dtos;
using Traceab.DTO;
using Traceability.Domain;
using Traceability.Service.Impl;
using Traceability.Service.Interfae;
using Yitter.IdGenerator;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;

namespace RBACDuYaJun.Api.Controllers
{
    /// <summary>
    /// 溯源管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TraceabilitysController : ControllerBase
    {
        /// <summary>
        /// 商品服务
        /// </summary>
        private readonly IGoodsService _goodsService;
        /// <summary>
        /// 图片服务
        /// </summary>
        private readonly IImgsService _imgsService;
        /// <summary>
        /// 溯源服务
        /// </summary>
        private readonly ITraceabilitysService _TracesService;

        public TraceabilitysController(IGoodsService goodsService, IImgsService imgsService, ITraceabilitysService tracsService)
        {
            _goodsService = goodsService;
            _imgsService = imgsService;
            _TracesService = tracsService;
        }
        /// <summary>
        /// 图片上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult CourseFileInfo(IFormFile file)
        {
            //图片 放在文件夹
            string savePath = Directory.GetCurrentDirectory() + @"\wwwroot\Pic\";
            //覆盖图片名
            string NewFileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
            //上传
            using (FileStream fs = new FileStream(savePath + NewFileName, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }
            return Ok(NewFileName);
        }
        
        /// <summary>
        /// 详情反填
        /// </summary>
        /// <param name="batch"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<TraceDto>> GetTraceabilitysBy(string batchs)
        {
           return await _TracesService.GetTraceabilitysBy(batchs);
        }
        /// <summary>
        /// 修改相对应的生产批次
        /// </summary>
        /// <param name="imgs"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateImgs(int Iid, string Xid)
        {
            return await _imgsService.UpdateImgs(Iid, Xid);
        }
        /// <summary>
        /// 显示产品检验报告图片
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetImgsAll(string batch, int tid)
        {
            return await _imgsService.GetImgsAll(batch,tid);
        }
        /// <summary>
        /// 显示养殖企业图片
        /// </summary>
        /// <param name="hid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetEndImgAll(string bacth, int hid)
        {
           return await _imgsService.GetEndImgAll(bacth,hid);
        }
        /// <summary>
        /// 显示动检证图片
        /// </summary>
        /// <param name="hid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetHesImgAll(string bacth,int hid)
        {
            return await _imgsService.GetHesImgAll(bacth,hid);
        }
        /// <summary>
        /// 显示屠宰相关资质图片
        /// </summary>
        /// <param name="sid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetSlImgAll(string bacth,int sid)
        {
          return await _imgsService.GetSlImgAll(bacth,sid);
        }
        /// <summary>
        /// 显示屠宰企业图片
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetSlpriseImgAll(string bacth, int pid)
        {
            return await _imgsService.GetSlpriseImgAll(bacth, pid);
        }
        /// <summary>
        /// 显示深加工食品安全图片
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetPressImgAll(string bacth,int pid)
        {
            return await _imgsService.GetPressImgAll(bacth,pid);
        }
        /// <summary>
        /// 显示原料图片
        /// </summary>
        /// <param name="bacth"></param>
        /// <param name="rid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetRawImgAll(int tid, int pid)
        {
            return await _imgsService.GetRawImgAll(tid,pid);
        }
        /// <summary>
        /// 显示辅料图片
        /// </summary>
        /// <param name="bacth"></param>
        /// <param name="Aid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetAuxImgAll(int tid, int pid)
        {
            return await _imgsService.GetAuxImgAll(tid, pid);
        }
        /// <summary>
        /// 显示添加剂图片
        /// </summary>
        /// <param name="bacth"></param>
        /// <param name="Additiveid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetAdditiveImgAll(int tid, int pid)
        {
            return await _imgsService.GetAdditiveImgAll(tid, pid);
        }
        /// <summary>
        /// 显示包材图片
        /// </summary>
        /// <param name="bacth"></param>
        /// <param name="Packagingid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetPackImgAll(int tid, int pid)
        {
           return await _imgsService.GetPackImgAll(tid, pid);
        }
        /// <summary>
        /// 显示深加工企业信息图片
        /// </summary>
        /// <param name="bacth"></param>
        /// <param name="Deepid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<ImgDto>> GetDeepImgAll(string bacth, int pid)
        {
           return await _imgsService.GetDeepImgAll(bacth,pid);
        }
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <param name="imgs"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddImgs(Imgs imgs)
        {
            return await _imgsService.AddImgs(imgs);
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="ImgName"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DelTracImg(string? ImgName)
        {
           return await _imgsService.DelTracImg(ImgName);
        }
        /// <summary>
        /// 雪花ID
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetSnowList(int count = 10)
        {
            List<long> snowList = new List<long>();
            for (int i = 0; i < count; i++)
            {
                long snowid = YitIdHelper.NextId();
                snowList.Add(snowid);
            }
            return Ok(snowList);
        }
        /// <summary>
        /// 显示商品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<Goodsd>> GetGoodsdsAsync()
        {
           return await _goodsService.GetGoodsdsAsync();
        }
       
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="traceabilitys"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> AddTraceabilitys(Traceabilitys traceabilitys)
        {
            return await _TracesService.AddTraceabilitys(traceabilitys);
        }
      
        /// <summary>
        /// 单删
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<int> DelTra(int id)
        {
           return await _TracesService.DelTra(id);

        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> DelTraceabilitys(List<int> ids)
        {
            return await _TracesService.DelTraceabilitys(ids);
        }
        /// <summary>
        /// 显示全部
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDto<TraceDto>>> GetTraceabilitys(int pageSize, int pageIndex)
        {
            ApiResult<PageDto<TraceDto>> result = new ApiResult<PageDto<TraceDto>>();
            try
            {
                result.Data=await _TracesService.GetTraceabilitys(pageSize, pageIndex);
               // result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }

            return result;
        }
        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Traceabilitys> GetTraceabilitysByid(int id)
        {
            return await _TracesService.GetTraceabilitysByid(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="traceabilitys"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> UpdateTraceabilitys(Traceabilitys traceabilitys)
        {
            return await _TracesService.UpdateTraceabilitys(traceabilitys);
        }
    }
}
